<?php

/**
 *  用户控制器 api
 * @author xiaojy  xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class UserController extends ApiController
{

    public $login_require = array('user.info', 'user.avatar', 'user.update', 'user.apply', 'user.couponlist', 'user.applye', 'user.applyp', 'user.applycarrye', 'user.applycarryp',
        'user.upaddress', 'user.carryinfo', 'user.carrylist', 'user.addcomment', 'user.commentlist', 'user.deletecomment', 'user.carryshopupdate', 'user.work', 'user.bind', 'user.unbind', 'user.carrycenter', 'user.signed', 'user.getapply');
    public $self_error_list = array(
        'passworderr' => array('errno' => 208, 'errmsg' => '密码错误'),
        'updateerr' => array('errno' => 302, 'errmsg' => '更新失败，可能是网络连接问题'),
        'applye' => array('errno' => 306, 'errmsg' => '你已认证，不能再次修改'),
        'captchaerr' => array('errno' => 303, 'errmsg' => '验证码错误'),
        'error_bind' => array('errno' => 307, 'errmsg' => '绑定错误'),
    );


    /**
     * @param $name 登录名 ，
     * @param  $password 密码
     * @param
     * @return boolean whether authentication succeeds.
     */
    public function actionLogin()
    {
        $this->status = $this->error_list['success'];
        $username = Yii::app()->request->getPost('mobile');
        $password = Yii::app()->request->getPost('password');
        $role = Yii::app()->request->getPost('role', 1);//app_id
        if (empty($username) || empty($password)) {
            $this->status = $this->error_list['need_param'];
            return;
        } else {
            $re = User::model()->verify($username, $password);
            if ($re['errno'] == 0) {
                //登录成功
                $sid = $re['result']['sid'];
                $new_token = UserToken::model()->genToken($re['result']['sid'], $role, true);
                $user = UserInfo::model()->getBySidToApi($sid);
                $user['result']['token'] = UserToken::model()->getTokenByUserId($re['result']['sid'], $role);
            } else {
                $this->status['errno'] = $re['errno'];
                $this->status['errmsg'] = $re['errmsg'];
                return;
            }
            $this->result = $user['result'];
        }
    }


    //绑定推送
    public function actionBind()
    {
        $user_id = $this->user_id;
        $push_user_id = Yii::app()->request->getParam('push_user_id');
        $platform = Yii::app()->request->getPost('platform');
        $client_id = Yii::app()->request->getParam('role');//1 车货匹配 2tms 

        $log = array();
        $log['user_id'] = $user_id;
        $log['platform'] = $platform;
        $log['role'] = $client_id;
        $log['push_user_id'] = $push_user_id;
        // file_put_contents(Yii::app()->getRuntimePath() . '/push_message_' . date("Y_W") . '.txt', sprintf("%s %s %s", date("Y-m-d H:i:s"), json_encode($log), PHP_EOL), FILE_APPEND);
        //
        if (empty($push_user_id) || empty($client_id)) {
            $this->status = $this->error_list['need_param'];
            if (YII_DEBUG && true) {
                $this->status['errmsg'] = 'push_user_id:' . $push_user_id . ' client_id:' . $client_id;
            }
            return;
        }
        if ($platform == 'ios') {
            $platform = 1;
        } else {
            $platform = 2;
        }
        //判断和原有push_user_id是否一致 没有 或者一致 不操作   不一致则发送消息 提示被挤下线
        $old_user_push_info = UserToken::model()->getByUserId($user_id);
        if (!empty($old_user_push_info)) {
            if ($old_user_push_info->user_push_id != $push_user_id) {

            }
        }


        $is_bind = UserToken::model()->newBind($user_id, $push_user_id, $platform, $client_id);
        if ($is_bind) {
            $this->status = $this->error_list['success'];
        } else {
            $this->status = $this->error_list['error_bind'];
        }
    }

    //取消绑定推送
    public function actionUnbind()
    {
        $user_id = $this->user_id;
        $push_user_id = Yii::app()->request->getPost('push_user_id');
        $client_id = Yii::app()->request->getParam('role');//1 跑男端 2用户端
        if (empty($push_user_id)) {
            $this->status = $this->error_list['need_param'];
            return;
        }
        $is_bind = UserToken::model()->unbind($user_id, $push_user_id, $client_id);
        if ($is_bind) {
            $this->status = $this->error_list['success'];
        } else {
            $this->status = $this->error_list['error'];
        }
    }


    //头像
    public function actionAvatar()
    {
        $sid = $this->user_id;
        $user = array();
        $user['sid'] = $sid;
        $user['avatar'] = Yii::app()->request->getPost('avatar');
        if (!empty($user['avatar'])) {
            $re = User::model()->updateUser($user);
            $result['avatar'] = WxUtil::getGoodsImgUrl($re['result']['avatar']);
            $this->result = $result;
            $this->status = $this->error_list['success'];
        } else {
            $this->status = $this->error_list['need_param'];
        }
    }


    //个人货主认证申请
    public function actionApplyp()
    {
        $data = array();
        $data['name'] = Yii::app()->request->getPost('name');
        $data['token'] = Yii::app()->request->getPost('token');
        $data['nickname'] = Yii::app()->request->getPost('nickname');
        $data['idcard'] = Yii::app()->request->getPost('idcard');
        $data['idcard_photo'] = Yii::app()->request->getPost('idcard_photo');
        $data['idcard_positive'] = Yii::app()->request->getPost('idcard_positive');
        $data['idcard_reverse'] = Yii::app()->request->getPost('idcard_reverse');
        $data['gender'] = Yii::app()->request->getPost('gender');
        $data['type'] = ApplyShipper::APPLY_P;
        $data['user_id'] = $this->user_id;
        if ($this->is_operation == 1 || $this->is_driver == 1) {
            $this->status['errno'] = 4533;
            $this->status['errmsg'] = '当前角色不能认证';
            return;
        }
        $commit_apply = ApplyShipper::model()->checkCommitApply($data['user_id'], ApplyShipper::APPLY_P);
        if ($commit_apply['errno'] == 0) {
        } else {
            $this->status['errno'] = $commit_apply['errno'];
            $this->status['errmsg'] = $commit_apply['errmsg'];
            return;
        }
        $result = ApplyShipper::model()->overDue($data);
        //print_r($data);exit;
        if ($result['errno'] == 0) {
            if (empty($data['name']) || empty($data['idcard_positive']) || empty($data['idcard_reverse'])
                || empty($data['idcard']) || empty($data['idcard_photo'])
                || empty($data['token']) || empty($data['gender'])
            ) {
                $this->status = $this->error_list['need_param'];
            } else {
                $re = ApplyShipper::model()->add($data);
                $this->status['errno'] = $re['errno'];
                $this->status['errmsg'] = $re['errmsg'];
            }
        } else {
            $this->status['errno'] = $result['errno'];
            $this->status['errmsg'] = $result['errmsg'];

        }
    }


    //企业货主认证申请
    public function actionApplye()
    {
        $data = array();
        $data['enterprise'] = Yii::app()->request->getPost('enterprise');
        $data['link_man'] = Yii::app()->request->getPost('link_man');
        $data['link_way'] = Yii::app()->request->getPost('link_way');
        $data['business_license'] = Yii::app()->request->getPost('business_license');
        $data['token'] = Yii::app()->request->getPost('token');
        $data['type'] = ApplyShipper::APPLY_E;
        $data['abbreviation'] = Yii::app()->request->getPost('abbreviation');
        $data['user_id'] = $this->user_id;
        if ($this->is_operation == 1 || $this->is_driver == 1) {
            $this->status['errno'] = 4533;
            $this->status['errmsg'] = '当前角色不能认证';
            return;
        }

        $commit_apply = ApplyShipper::model()->checkCommitApply($data['user_id'], ApplyShipper::APPLY_E);
        if ($commit_apply['errno'] == 0) {
        } else {
            $this->status['errno'] = $commit_apply['errno'];
            $this->status['errmsg'] = $commit_apply['errmsg'];
            return;
        }

        $result = ApplyShipper::model()->overDue($data);
        if (empty($data['enterprise']) || empty($data['link_man']) || empty($data['link_way'])
            || empty($data['business_license']) || empty($data['token'])
        ) {
            $this->status = $this->error_list['need_param'];
        } else {
            $re = ApplyShipper::model()->add($data);
            $this->status['errno'] = $re['errno'];
            $this->status['errmsg'] = $re['errmsg'];

        }
    }

    //个人承运人认证申请
    public function actionApplycarryp()
    {
        $data = array();
        $data['name'] = Yii::app()->request->getPost('name');
        $data['nickname'] = Yii::app()->request->getPost('nickname');
        $data['driving_licence'] = Yii::app()->request->getPost('driving_licence');
        $data['transport_licence'] = Yii::app()->request->getPost('transport_licence');
        $data['idcard'] = Yii::app()->request->getPost('idcard');
        $data['idcard_photo'] = Yii::app()->request->getPost('idcard_photo');
        $data['trailer_driving_licence'] = Yii::app()->request->getPost('trailer_driving_licence');
        $data['headstock_photo'] = Yii::app()->request->getPost('headstock_photo');
        $data['trailer_photo'] = Yii::app()->request->getPost('trailer_photo');//挂车照
        $data['trailer_card'] = Yii::app()->request->getPost('trailer_card');//挂车车牌
        //$data['gender'] = Yii::app()->request->getPost('gender');
        $data['type'] = ApplyShipper::APPLY_CARRY_P;
        $data['car_driving_licence'] = Yii::app()->request->getPost('car_driving_licence');
        $data['car_type'] = Yii::app()->request->getPost('car_type');
        $data['car_load'] = Yii::app()->request->getPost('car_load');
        $data['car_long'] = Yii::app()->request->getPost('car_long');
        $data['car_volume'] = Yii::app()->request->getPost('car_volume');
        $data['car_width'] = Yii::app()->request->getPost('car_width');
        $data['car_height'] = Yii::app()->request->getPost('car_height');
        $data['user_id'] = $this->user_id;
        if ($this->is_operation == 1 || $this->is_driver == 1) {
            $this->status['errno'] = 4533;
            $this->status['errmsg'] = '当前角色不能认证';
            return;
        }
        $commit_apply = ApplyShipper::model()->checkCommitApply($data['user_id'], ApplyShipper::APPLY_CARRY_P);
        if ($commit_apply['errno'] == 0) {
        } else {
            $this->status['errno'] = $commit_apply['errno'];
            $this->status['errmsg'] = $commit_apply['errmsg'];
            return;
        }
        $result = ApplyShipper::model()->overDue($data);
        if (empty($data['name']) || empty($data['driving_licence']) || empty($data['transport_licence'])
            || empty($data['idcard']) || empty($data['idcard_photo'])
            || empty($data['car_driving_licence'])
            || empty($data['car_type']) || empty($data['car_load'])
            || empty($data['car_long']) || empty($data['headstock_photo'])
        ) {

            $this->status = $this->error_list['need_param'];
        } else {
            $re = ApplyShipper::model()->add($data);
            $this->status['errno'] = $re['errno'];
            $this->status['errmsg'] = $re['errmsg'];

        }

    }


    //企业承运人认证申请
    public function actionApplycarrye()
    {
        $data = array();
        $data['name'] = Yii::app()->request->getPost('enterprise');
        $data['link_man'] = Yii::app()->request->getPost('link_man');
        $data['link_way'] = Yii::app()->request->getPost('link_way');
        $data['abbreviation'] = Yii::app()->request->getPost('abbreviation');
        $data['business_license'] = Yii::app()->request->getPost('business_license');
        $data['transport_licence'] = Yii::app()->request->getPost('transport_licence');
        $data['transport_licence_id'] = Yii::app()->request->getPost('transport_licence_id');
        $data['credit_code'] = Yii::app()->request->getPost('credit_code');
        $data['type'] = ApplyShipper::APPLY_CARRY_E;
        $data['user_id'] = $this->user_id;
        if ($this->is_operation == 1 || $this->is_driver == 1) {
            $this->status['errno'] = 4533;
            $this->status['errmsg'] = '当前角色不能认证';
            return;
        }
        $commit_apply = ApplyShipper::model()->checkCommitApply($data['user_id'], ApplyShipper::APPLY_CARRY_E);
        if ($commit_apply['errno'] == 0) {
        } else {
            $this->status['errno'] = $commit_apply['errno'];
            $this->status['errmsg'] = $commit_apply['errmsg'];
            return;
        }
        $result = ApplyShipper::model()->overDue($data);
        if (empty($data['name']) || empty($data['link_man']) || empty($data['link_way'])
            || empty($data['business_license'])
        ) {
            $this->status = $this->error_list['need_param'];
        } else {
            $re = ApplyShipper::model()->add($data);
            $this->status['errno'] = $re['errno'];
            $this->status['errmsg'] = $re['errmsg'];
        }
    }

    //承运人列表
    public function actionCarrylist()
    {
        $filter = array();
        $filter['name'] = Yii::app()->request->getQuery('name'); // 承运人名称
        //$filter['user_sid'] = Yii::app()->request->getQuery('user_sid'); //
        $filter['city_id'] = Yii::app()->request->getQuery('city_id');
        $filter['province_id'] = Yii::app()->request->getQuery('province_id');

        $address = array();
        $address['province_id'] = $filter['province_id'];
        $address['city_id'] = $filter['city_id'];
        if (!empty($address['province_id']) || !empty($address['city_id'])) {
            $carry_ids_addr = array();//根据地址找到的承运人id数组
            $carry_ids_addr = CarryShop::model()->getSidByAddr($address);
            if (isset($carry_ids)) {
                $carry_ids = array_intersect($carry_ids, $carry_ids_addr);
            } else {
                $carry_ids = $carry_ids_addr;
            }
        }

        if (isset($carry_ids)) {    //筛选条件
            $filter['ids'] = $carry_ids;
        }
        $not_in = Yii::app()->request->getQuery('self', 0);    //1不包括自己

        if ($not_in == 1) {
            $log = array();
            $log[] = $this->company_id;
            $filter['not_in'] = $log;
        }
        $filter['style'] = Yii::app()->request->getQuery('style', 1);
        $filter['page'] = Yii::app()->request->getQuery('page', 1);
        $filter['page_size'] = Yii::app()->request->getQuery('page_size', 20);
        $filter['type'] = Yii::app()->request->getQuery('type');
        $filter['keyword'] = Yii::app()->request->getQuery('keyword');
        $re = CarryShop::model()->listByAttr($filter);
        $this->status['errno'] = $re['errno'];
        $this->status['errmsg'] = $re['errmsg'];
        $temp = array();
        foreach ($re['result']['data'] as $key => $value) {
            $temp[$key]['id'] = $value['user_sid'];
            $temp[$key]['score'] = CarryShop::model()->dealScore($value['score']);
            $temp[$key]['user_sid'] = $value['user_sid'];
            $temp[$key]['type'] = (int)$value['type'];
            $temp[$key]['name'] = strval($value['name']);
            if ($value['type'] == 4) {
                $temp[$key]['head'] = strval($value['head']);
                $temp[$key]['head_link_way'] = strval($value['head_link_way']);
            } else {
                $temp[$key]['head'] = strval($value['name']);
                $temp[$key]['head_link_way'] = User::model()->getMobileBySid($value['user_sid']);
            }
            $temp[$key]['car_num'] = (int)$value['car_num'];
            $temp[$key]['order_num'] = (int)$value['order_num'];
            if (!empty($value['avatar'])) {
                $temp[$key]['avatar'] = WxUtil::getGoodsImgUrl($value['avatar']);
            } else {
                $temp[$key]['avatar'] = User::model()->getAvatar($value['user_sid']);
            }
            $province_text = Province::model()->getNameByProvinceId($value['province_id']);
            $city_text = City::model()->getNameByCityId($value['city_id']);
            $temp[$key]['address'] = $province_text . $city_text;
            if (!empty(Favorite::model()->getInfoByIdandSid($this->company_id, $value['user_sid']))) {
                $temp[$key]['favorite'] = 1;
            } else {
                $temp[$key]['favorite'] = 0;
            }
        }

        $this->result = $temp;
    }


    //承运人详情
    public function actionCarrydetail()
    {
        $sid = Yii::app()->request->getQuery('sid');
        $re = CarryShop::model()->getBySid($sid);
        $this->status['errno'] = $re['errno'];
        $this->status['errmsg'] = $re['errmsg'];
        $carry_shop_detail = ArrayComponent::unsetNullArr($re['result']);    //将数组中null递归转换成'';
        $this->result = $carry_shop_detail;
    }

    //承运人详情
    public function actionCarryinfo()
    {

        $sid = Yii::app()->request->getParam('sid');
        $re = CarryShop::model()->getBySid($sid);
        $this->status['errno'] = $re['errno'];
        $this->status['errmsg'] = $re['errmsg'];
        if ($re['errno'] == 0) {
            $temp = array();
            $temp['score'] = CarryShop::model()->dealScore($re['result']['score']);
            $temp['user_sid'] = $re['result']['user_sid'];
            $temp['type'] = $re['result']['type'];
            $temp['name'] = strval($re['result']['name']);

            if ($re['result']['type'] == 4) {
                $temp['head'] = strval($re['result']['head']);
                $temp['head_link_way'] = strval($re['result']['head_link_way']);
            } else {
                $temp['head'] = strval($re['result']['name']);
                $temp['head_link_way'] = User::model()->getMobileBySid($re['result']['user_sid']);
            }

            $temp['car_num'] = (int)$re['result']['car_num'];
            $temp['order_num'] = (int)$re['result']['order_num'];
            if (!empty($re['result']['avatar'])) {
                $temp['avatar'] = WxUtil::getGoodsImgUrl($re['result']['avatar']);
            } else {
                $temp['avatar'] = User::model()->getAvatar($re['result']['user_sid']);
            }

            $user_info = User::model()->getBySid($sid);
            // $province_text = Province::model()->getNameByProvinceId($user_info['result']['province_id']);
            // $city_text = City::model()->getNameByCityId($user_info['result']['city_id']);
            // $temp['address'] = $province_text.$city_text;
            if ($user_info['errno'] == 0) {
                $temp['address'] = $user_info['result']['province_text'] . $user_info['result']['city_text'];
            } else {
                $temp['address'] = '';
            }

            $favorite = Favorite::model()->getInfoByIdandSid($this->user_id, $sid);
            if (!empty($favorite)) {
                $temp['favorite'] = 1;
            } else {
                $temp['favorite'] = 0;
            }
            $this->result = $temp;
        }
    }

    //个人承运人中心
    public function actionCarrycenter()
    {
        $sid = $this->user_id;
        $user = User::model()->getBySid($this->user_id);
        $carry = CarryShop::model()->getBySid($sid);

        $this->status['errno'] = 0;
        $this->status['errmsg'] = 'success';
        if ($user['errno'] != 0) {
            $this->status['errno'] = $user['errno'];
            $this->status['errmsg'] = $user['errmsg'];
            return;
        }

        $temp = array();
        $apply_status = array();
        $apply_status['personshipper'] = ApplyShipper::model()->getApplyStatus($this->user_id, ApplyShipper::APPLY_P);
        $apply_status['enterpriseshipper'] = ApplyShipper::model()->getApplyStatus($this->user_id, ApplyShipper::APPLY_E);
        $apply_status['personcarry'] = ApplyShipper::model()->getApplyStatus($this->user_id,
            ApplyShipper::APPLY_CARRY_P);
        $apply_status['enterprisecarry'] = ApplyShipper::model()->getApplyStatus($this->user_id, ApplyShipper::APPLY_CARRY_E);
        $temp['apply_status'] = $apply_status;
        $temp['mobile'] = $user['result']['mobile'];
        $temp['apply'] = 0;
        $temp['integral'] = $user['result']['integral'];
        $temp['name'] = strval($user['result']['name']);
        $temp['idcard'] = strval($user['result']['idcard']);
        $province_text = Province::model()->getNameByProvinceId($user['result']['province_id']);
        $city_text = City::model()->getNameByCityId($user['result']['city_id']);
        $temp['address'] = $province_text . $city_text;
        $temp['is_driver'] = $user['result']['is_driver'];
        $temp['token'] = UserToken::model()->getTokenByUserId($this->user_id, 2);
        $temp['balance'] = (double)$user['result']['balance'];
        $temp['avatar'] = User::model()->getAvatar($this->user_id);
        $temp['signed_log'] = SignedLog::model()->checkSign($this->user_id);
        if (!empty($user['result']['nickname'])) {
            $temp['nickname'] = strval($user['result']['nickname']);
        } else {
            $temp['nickname'] = strval($user['result']['nickname']);
        }

        if ($carry['errno'] == 0) {

            $temp['score'] = CarryShop::model()->dealScore($carry['result']['score']);
            $temp['user_sid'] = $carry['result']['user_sid'];
            $temp['apply'] = $carry['result']['type'];
            $temp['name'] = strval($carry['result']['name']);

            if ($carry['result']['type'] == CarryShop::ENTERPRISE_CARRY_SHOP) {
                $temp['link_man'] = strval($carry['result']['head']);
                $temp['link_way'] = strval($carry['result']['head_link_way']);
            } else {
                $temp['link_man'] = strval($carry['result']['name']);
                $temp['link_way'] = User::model()->getMobileBySid($carry['result']['user_sid']);
            }

            $temp['transport_licence_id'] = strval($carry['result']['transport_licence_id']);
            $temp['integral'] = (int)$user['result']['integral'];
            $temp['car_num'] = (int)$carry['result']['car_num'];
            $temp['order_num'] = (int)$carry['result']['order_num'];
            if (!empty($carry['result']['avatar'])) {
                $temp['avatar'] = WxUtil::getGoodsImgUrl($carry['result']['avatar']);
            } else {
                $temp['avatar'] = User::model()->getAvatar($carry['result']['user_sid']);
            }

            $this->result = $temp;
        } else {

            $driver_id = $this->user_id;
            $driver = Driver::model()->getBySid($driver_id);
            if ($driver['errno'] == 0) {
                $this->status['errno'] = 0;
                $this->status['errmsg'] = $driver['errmsg'];
                $driver_info = $driver['result'];
                $temp['idcard'] = $driver_info['idcard'];
                $temp['mobile'] = $driver_info['mobile'];
                $temp['name'] = $driver_info['name'];
                $temp['apply'] = 0;
                $temp['account_name'] = $driver_info['account_name'];
                $temp['bank_card'] = $driver_info['bank_card'];
                $temp['open_bank'] = $driver_info['open_bank'];
                $temp['integral'] = (int)$user['result']['integral'];
                $temp['avatar'] = User::model()->getAvatar($this->user_id);

                $this->result = $temp;
            } else {

                $this->result = $temp;
                // $this->status['errno'] = 3454;
                // $this->status['errmsg'] = '司机信息不存在';
            }


        }
    }

    //承运人信息修改
    public function actionCarryshopupdate()
    {

        $data = array();
        $data['user_sid'] = $this->user_id;
        $data['head'] = Yii::app()->request->getPost('link_man');
        $data['head_link_way'] = Yii::app()->request->getPost('link_way');

        //$sid = Yii::app()->request->getQuery('sid');
        $re = CarryShop::model()->edit($data);
        $this->status['errno'] = $re['errno'];
        $this->status['errmsg'] = $re['errmsg'];
    }


    public function actionAddcomment()
    {
        $data = array();
        $data['from_user_id'] = $this->user_id;
        $users = Yii::app()->request->getPost('to_user_id', array());    //对谁评论数组
        if (empty($users)) {
            $this->status['errno'] = 3467;
            $this->status['errmsg'] = '参数不足';
            return;
        }
        $data['score'] = Yii::app()->request->getPost('score');
        $data['order_id'] = Yii::app()->request->getPost('order_id');
        $data['comment'] = Yii::app()->request->getPost('comment');
        $flag = 0;
        foreach ($users as $key => $value) {
            $data['to_user_id'] = $value;
            $comment = Comment::model()->add($data);
            if ($comment['errno'] != 0) {
                file_put_contents(Yii::app()->getRuntimePath() . '/addcomment' . date("Y_W") . '.txt', sprintf("%s %s %s %s", date('Y-m-d H:i:s'), $value, CJSON::encode($comment['errmsg']), PHP_EOL), FILE_APPEND);
            } else {
                $flag++;
            }
        }
        if ($flag > 0) {
            Order::model()->chengeCommentStatus($data['order_id']);
        }
        $this->status['errno'] = $comment['errno'];
        $this->status['errmsg'] = $comment['errmsg'];
    }

    public function actionCommentlist()
    {
        $data = array();
        $type = Yii::app()->request->getQuery('type', 1);
        $filter = array();

        if ($type == 1) {
            $filter['to_user_id'] = $this->user_id;
        } elseif ($type == 2) {
            $filter['from_user_id'] = $this->user_id;
            $filter['to_user_id'] = Yii::app()->request->getQuery('to_user_id');
        } elseif ($type == 3) {
            $filter['to_user_id'] = Yii::app()->request->getQuery('to_user_id');
            if (empty($filter['to_user_id'])) {
                $this->status['errno'] = 3154;
                $this->status['errmsg'] = '需要指定用户';
                return;
            }
        } else {
            $this->status['errno'] = 3155;
            $this->status['errmsg'] = '类型错误';
            return;
        }
        $filter['page'] = Yii::app()->request->getQuery('page', 1);
        $filter['page_size'] = Yii::app()->request->getQuery('page_size', 20);
        $filter['style'] = 'ctime desc';
        $comment_list = Comment::model()->listByAttr($filter);
        $this->status['errno'] = $comment_list['errno'];
        $this->status['errmsg'] = $comment_list['errmsg'];
        $temp = array();
        if ($comment_list['errno'] == 0) {
            foreach ($comment_list['result']['data'] as $key => $value) {
                $temp[$key]['id'] = $value['id'];
                $temp[$key]['from_user_id'] = $value['from_user_id'];
                $temp[$key]['to_user_id'] = $value['to_user_id'];
                $temp[$key]['order_id'] = strval($value['order_id']);
                $temp[$key]['comment'] = $value['comment'];
                $temp[$key]['score'] = CarryShop::model()->dealScore($value['score']);
                $temp[$key]['user_name'] = $value['user_name'];
                $temp[$key]['ctime'] = $value['ctime'];
                $temp[$key]['ctime_text'] = date('Y-m-d H:i:s', $value['ctime']);
                $temp[$key]['avatar'] = User::model()->getAvatar($value['from_user_id']);
            }
            $this->result = $temp;
        }

    }

    public function actionDeletecomment()
    {
        $id = Yii::app()->request->getPost('id');
        if (!empty($id)) {
            $from_user_id = $this->user_id;
            $delete_info = Comment::model()->deleteById($id, $from_user_id);
            $this->status['errno'] = $delete_info['errno'];
            $this->status['errmsg'] = $delete_info['errmsg'];
        } else {
            $this->status = $this->error_list['need_param'];
        }
    }

    //用户信息
    public function actionInfo()
    {
        $re = User::model()->getBySid($this->user_id);
        $apply_status = array();
        $apply_status['personshipper'] = ApplyShipper::model()->getApplyStatus($this->user_id, ApplyShipper::APPLY_P);
        $apply_status['enterpriseshipper'] = ApplyShipper::model()->getApplyStatus($this->user_id, ApplyShipper::APPLY_E);
        $apply_status['personcarry'] = ApplyShipper::model()->getApplyStatus($this->user_id,
            ApplyShipper::APPLY_CARRY_P);
        $apply_status['enterprisecarry'] = ApplyShipper::model()->getApplyStatus($this->user_id, ApplyShipper::APPLY_CARRY_E);

        $result = array();
        if ($re['errno'] == 0) {
            $result['token'] = UserToken::model()->getTokenByUserId($this->user_id, 1);
            $result['name'] = strval($re['result']['name']);
            $result['enterprise'] = strval($re['result']['enterprise']);
            $result['balance'] = (double)$re['result']['balance'];
            if (!empty($re['result']['nickname'])) {
                $result['nickname'] = strval($re['result']['nickname']);
            } else {
                $result['nickname'] = strval($re['result']['mobile']);
            }
            $result['signed_log'] = SignedLog::model()->checkSign($this->user_id);
            $result['score'] = CarryShop::model()->dealScore($re['result']['score']);
            $result['mobile'] = strval($re['result']['mobile']);
            $result['gender'] = strval($re['result']['gender']);
            $result['link_man'] = strval($re['result']['link_man']);
            $result['link_way'] = strval($re['result']['link_way']);
            $result['enterprise'] = strval($re['result']['enterprise']);
            $result['avatar'] = User::model()->getAvatar($this->user_id);
            $result['business_license'] = WxUtil::getGoodsImgUrl($re['result']['business_license']);
            $result['idcard'] = strval($re['result']['idcard']);

            $cards = $re['result']['idcard_photo'];
            if (!empty($cards)) {
                $result['idcard_photo'] = WxUtil::getGoodsImgUrl($cards);
            } else {
                $result['idcard_photo'] = '';
            }
            $result['apply_status'] = $apply_status;
            $result['sid'] = $re['result']['sid'];
            $result['token'] = UserToken::model()->getTokenByUserId($re['result']['sid']);
            // if ($role == 2) {
            $result['authentication'] = $re['result']['authentication'];
            $result['apply'] = CarryShop::model()->getCarryType($re['result']['sid']);
            $province_text = Province::model()->getNameByProvinceId($re['result']['province_id']);
            $city_text = City::model()->getNameByCityId($re['result']['city_id']);
            $result['address'] = $province_text . $city_text;
            //  }
            // $result['car_num'] = strval($re['result']['car_num']);
            //$result['order_num'] = strval($re['result']['order_num']);
            $result['integral'] = (int)$re['result']['integral'];
            // }
            $this->result = $result;
            $this->status = $this->error_list['success'];
        } else {
            $this->status['errno'] = $re['errno'];
            $this->status['errmsg'] = $re['errmsg'];

        }

    }


    public function actionUpdate()
    {

        $this->status = $this->error_list['success'];
        $user = array();
        $sid = $this->user_id;
        $user['sid'] = $sid;
        $user['name'] = Yii::app()->request->getPost('name');
        $user['avatar'] = Yii::app()->request->getPost('avatar');
        $user['nickname'] = Yii::app()->request->getPost('nickname');
        $user['area_id'] = Yii::app()->request->getPost('area_id');
        $user['province_id'] = Yii::app()->request->getPost('province_id');
        $user['city_id'] = Yii::app()->request->getPost('city_id');
        $re = User::model()->updateUser($user);
        if ($re['errno'] == 0) {
            $result = array();
            $result['token'] = UserToken::model()->getTokenByUserId($re['result']['sid']);
            if (!empty($re['result']['nickname'])) {
                $result['nickname'] = strval($re['result']['nickname']);
            } else {
                $result['nickname'] = strval($re['result']['mobile']);
            }
            $result['avatar'] = User::model()->getAvatar($re['result']['sid']);
            $result['province_id'] = strval($re['result']['province_id']);
            $result['city_id'] = strval($re['result']['city_id']);
            $result['area_id'] = strval($re['result']['area_id']);
            $this->result = $result;
        }
    }

    /**
     * @param  post提交 用户信息数组，电话，密码。captcha 短信验证码
     * @return bool
     *
     */
    public function actionSignup()
    {
        $this->status = $this->error_list['success'];
        $user['mobile'] = Yii::app()->request->getPost('mobile');
        $user['captcha'] = Yii::app()->request->getPost('captcha');
        $user['password'] = Yii::app()->request->getPost('password');
        $user['app_id'] = $this->app_id;
        if (empty($user['mobile']) || empty($user['password']) || empty('captcha')) {
            $this->status = $this->error_list['need_param'];
        } else {
            if (User::model()->checkCaptcha($user['captcha'], $user['mobile'])) {
                $re = User::model()->signUp($user);
                if ($re['errno'] == 0) {
                    $this->status = $this->error_list['success'];
                    $this->result = $re['result'];
                } else {
                    $this->status['errno'] = $re['errno'];
                    $this->status['errmsg'] = $re['errmsg'];
                    $this->result = $re['result'];
                }
            } else {
                $this->status = $this->error_list['captchaerr'];
            }
        }
    }

    //重置密码
    public function actionResetpassword()
    {
        $user['mobile'] = Yii::app()->request->getPost('mobile');
        $user['password'] = Yii::app()->request->getPost('password');
        $user['captcha'] = Yii::app()->request->getPost('captcha');
        if (empty($user['mobile']) || empty($user['password']) || empty('captcha')) {
            $this->status = $this->error_list['need_param'];
        } else {
            $re = User::model()->retrievePassword($user);
            if ($re['errno'] == 0) {
                $this->status = $this->error_list['success'];
            } else {
                $this->status['errno'] = $re['errno'];
                $this->status['errmsg'] = $re['errmsg'];
            }
        }
    }

    //签到
    public function actionSigned()
    {
        $user_id = $this->user_id;
        $integral = 0;
        $role = Yii::app()->request->getParam('role');
        $result = SignedLog::model()->add($user_id);
        if ($result['errno'] == 0) {
            # TODO....加积分 
            $integral = Config::model()->get('signed_integral', 0);
            $integralInfo = array();
            $integralInfo['type'] = IntegralFlow::SIGNED;
            $integralInfo['extend'] = '';
            $integralInfo['role'] = $role;
            User::model()->addIntegral($user_id, $integral, $integralInfo);
        } else {
            $this->status = $result;
            return;
        }
        $now_integral = 0;
        $user_info = User::model()->getBySid($user_id);
        if ($user_info['errno'] == 0) {
            $now_integral = $user_info['result']['integral'];
        }
        $this->status = $result;

        $this->result['integral'] = $now_integral;
        $this->result['message'] = '签到成功';
        if ($integral > 0) {
            $this->result['message'] .= ',积分+' . $integral;
        }
    }

    //获取历史认证
    public function actionGetapply()
    {
        $type = Yii::app()->request->getPost('type');    //1个人，2企业

        $base_info = array();
        if ($type == 1) {
            if ($this->shipper_type == 1) {
                $base_info = ApplyShipper::model()->findByUserApplyYes($this->user_id, ApplyShipper::APPLY_P);
            }

        } elseif ($type == 2) {
            if ($this->shipper_type == 2) {
                $base_info = ApplyShipper::model()->findByUserApplyYes($this->user_id, ApplyShipper::APPLY_E);

            }
        } else {
            $base_info = array();
        }


        $this->status = $this->error_list['success'];
        $this->result = $base_info;
    }

}

?>
